package com.foreverwin.mes.warehouse.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.foreverwin.mes.common.enums.HandleEnum;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.DateUtil;
import com.foreverwin.mes.warehouse.constant.Constants;
import com.foreverwin.mes.warehouse.dto.InventoryReceiveConfirmDto;
import com.foreverwin.mes.warehouse.mapper.InventoryReceiveConfirmMapper;
import com.foreverwin.mes.warehouse.model.InventoryReceiveConfirm;
import com.foreverwin.mes.warehouse.service.InventoryReceiveConfirmService;
import com.sap.me.extension.Services;
import com.sap.me.frame.transitionutils.Exceptions;
import com.sap.me.inventory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 库存接收确认 服务实现类
 * </p>
 *
 * @author Ervin Chen
 * @since 2020-02-12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InventoryReceiveConfirmServiceImpl extends ServiceImpl<InventoryReceiveConfirmMapper, InventoryReceiveConfirm> implements InventoryReceiveConfirmService {


    @Autowired
    private InventoryReceiveConfirmMapper inventoryReceiveConfirmMapper;

    @Override
    public Page<InventoryReceiveConfirm> selectPage(FrontPage<InventoryReceiveConfirm> frontPage, InventoryReceiveConfirm inventoryReceiveConfirm) {
        EntityWrapper<InventoryReceiveConfirm> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(inventoryReceiveConfirm);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<InventoryReceiveConfirm> selectList(InventoryReceiveConfirm inventoryReceiveConfirm) {
        EntityWrapper<InventoryReceiveConfirm> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(inventoryReceiveConfirm);
        return super.selectList(queryWrapper);
    }

    @Override
    public List<InventoryReceiveConfirmDto> selectInventoryReceiveConfirmList(InventoryReceiveConfirm inventoryReceiveConfirm) {
        EntityWrapper<InventoryReceiveConfirm> queryWrapper = new EntityWrapper<>();

        Date startDate = inventoryReceiveConfirm.getIssueDateTime();
        if (startDate != null) {
            Date endDate = DateUtil.addDays(startDate, 1);

            queryWrapper.between("issue_date_time", startDate, endDate);
            inventoryReceiveConfirm.setIssueDateTime(null);
        }
        inventoryReceiveConfirm.setIsConfirmed(Constants.NOT_CONFIRMED);
        queryWrapper.setEntity(inventoryReceiveConfirm);
        queryWrapper.orderAsc(Arrays.asList("issue_date_time","inventory_id"));

        Locale locale = LocaleContextHolder.getLocale();
        return inventoryReceiveConfirmMapper.selectInventoryReceiveConfirmList(locale.getLanguage(), queryWrapper);
    }

    @Override
    public boolean insertOrUpdate(List<InventoryReceiveConfirm> inventoryReceiveConfirms) {
        String site = CommonMethods.getSite();
        String userBo = CommonMethods.getUserBo();
        Date now = new Date();

        for (InventoryReceiveConfirm inventoryReceiveConfirm : inventoryReceiveConfirms) {

            inventoryReceiveConfirm.setIsConfirmed(Constants.CONFIRMED);
            inventoryReceiveConfirm.setUpdateUserBo(userBo);
            inventoryReceiveConfirm.setUpdatedDateTime(now);
            insertOrUpdate(inventoryReceiveConfirm);

            InventoryServiceInterface inventoryServiceInterface = Services.getService("com.sap.me.inventory", "InventoryService", site);
            Inventory inventory = null;
            try {
                String inventoryId = inventoryReceiveConfirm.getInventoryId();
                inventory = inventoryServiceInterface.findInventory(new InventoryIdentifier(inventoryId));
                if (inventory != null) {
                    InventoryValidateAndUpdateRequest updateRequest = new InventoryValidateAndUpdateRequest();
                    updateRequest.setInventoryId(inventory.getInventoryId());
                    updateRequest.setItemRef(inventory.getItemRef());
                    updateRequest.setQuantityOnHand(inventory.getQtyOnHand());
                    updateRequest.setStatus(InventoryStatusEnum.AVAILABLE);
                    updateRequest.setReasonCodeRef(HandleEnum.REASON_CODE.getHandle(site, "RTN-INV_CHANGE", "I"));
                    updateRequest.setComments("物料接收确认");
                    updateRequest.setAssyDataTypeRef(inventory.getAssyDataTypeRef());
                    updateRequest.setOperationLocationRef(inventory.getOperationLocRef());
                    updateRequest.setOperationLocationReserve(inventory.isOperationLocRes());
                    updateRequest.setShopOrderLocationRef(inventory.getShopOrderLocRef());
                    updateRequest.setShopOrderLocationReserve(inventory.isShopOrderLocRes());
                    updateRequest.setWorkCenterLocationRef(inventory.getWorkCenterLocRef());
                    updateRequest.setWorkCenterLocationReserve(inventory.isWorkCenterLocRes());
                    updateRequest.setResourceLocationRef(inventory.getResourceLocRef());
                    updateRequest.setResourceLocationReserve(inventory.isResourceLocRes());
                    updateRequest.setStorageLocationRef(inventory.getStorageLocationRef());
                    updateRequest.setModifiedDateTime(inventory.getModifiedDateTime());
                    updateRequest.setAssemblyDataList(inventory.getAssemblyDataList());
                    inventoryServiceInterface.validateAndUpdate(updateRequest);
                }
            } catch (com.sap.me.frame.domain.BusinessException e) {
                throw BusinessException.build(Exceptions.convert(e).toString());
            }
        }

        return true;
    }
}